package com.book.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.book.config.Message;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("reader")
public class ReaderController {

    @Autowired
    private Message message;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("sendMessage")
    public void sendMessage(@RequestParam String phone){

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(message.getAccessKeyId())
                .accessKeySecret(message.getAccessKeySecret())
                .build());


        AsyncClient client = AsyncClient.builder()
                .region(message.getRegion()) // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(message.getSetEndpointOverride())
                )
                .build();

        //生成验证码
        String code = RandomUtil.randomNumbers(6);
        redisTemplate.boundValueOps("sendMsg").set(code, Duration.ofMinutes(5));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);

        String s = JSONObject.toJSONString(jsonObject);

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName(message.getSignName())
                .templateCode(message.getTemplateCode())
                .phoneNumbers(phone)
                .templateParam(s)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(new Gson().toJson(resp));
        client.close();
    }

    public static void main(String[] args) {
        String code = RandomUtil.randomNumbers(6);
        System.out.println(code);
    }
}
